/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: MutilTableInterceptor
 * @Package com.rx.core.dbase
 * @Description: 双表内联
 * @author: 陈锦韬
 * @date: 2021\7\22 0022
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.dbase.mutil;

import com.rx.core.dbase.AbstractSqlHandler;
import com.rx.core.dbase.mutil.param.DoubleCopyInsertParameter;
import com.rx.core.util.EmptyChecker;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Properties;

/**
 * @author: Administrator
 * @Description: 双表内联
 * @date: 2021\7\22 0022
 */
@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})
@Slf4j
public class MutilTableInsertInterceptor extends AbstractSqlHandler implements Interceptor {

    private Object getValueFormHashMap(HashMap map,String name){
        try{
            if (EmptyChecker.isEmpty(map)){
                return null;
            }
            return map.get(name);
        }catch (Exception e){

        }
        return null;
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] queryArgs = invocation.getArgs();
        Object parameter = queryArgs[1];
        MappedStatement ms = (MappedStatement)queryArgs[0];
        SqlCommandType sqlCommandType = ms.getSqlCommandType();
        if (SqlCommandType.INSERT.equals(sqlCommandType)){
            if (parameter instanceof DoubleCopyInsertParameter){
                String sql = ((DoubleCopyInsertParameter) parameter).getSql();
                // 老sql
                BoundSql boundSql = ms.getBoundSql(parameter);
                Connection connection = null;
                PreparedStatement preparedStatement = null;
                try{
                    connection = ms.getConfiguration().getEnvironment().getDataSource().getConnection();
                    log.info("sql:" + sql);
                    preparedStatement = connection.prepareStatement(sql);
                    // 设置参数
                    ParameterHandler parameterHandler = new DefaultParameterHandler(ms,parameter,boundSql);
                    parameterHandler.setParameters(preparedStatement);
                    // 获取结果集
                    int count = preparedStatement.executeUpdate();
                    return count;
                }catch (Exception e){
                    log.error(e.getMessage());
                }finally {
                    closePrepare(preparedStatement);
                    preparedStatement = null;

                    closeConnect(connection);
                    connection = null;
                }
            }
        }

        return invocation.proceed();
    }

    /**
     * 产生代理对象
     * @param target
     * @return
     */
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        log.info("MutilTableInsertInterceptor：设置配置");
    }
}
